<template>
  <div class="layout-all">
    <control :config="config" />
    <div class="layout-header" style="float: left">
      <router-link to="./form/">
        <el-button type="primary" icon="el-icon-plus" style="margin-left: 0px"
          >添加</el-button
        >
      </router-link>
    </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%; margin-top: 50px"
      >
      <el-table-column align="center" prop="id" label="序号" width="70" :fixed="isMobile()?'left':undefined"/>
        <el-table-column align="center" prop="title" label="标题" />
        <el-table-column
          align="center"
          prop="team_title"
          label="团长姓名"
          width="150"
        />
        <el-table-column
          align="center"
          prop="team_phone"
          label="团长电话"
          width="150"
        />
        <el-table-column
          align="center"
          prop="divide_ratio"
          label="分润比(%)"
          width="100"
        />
        <el-table-column
          align="center"
          prop="brokerage"
          label="累计佣金"
          width="100"
        />
        <el-table-column
          align="center"
          prop="consignee"
          label="收货人"
          width="80"
        />
        <el-table-column
          align="center"
          prop="address"
          label="地址"
          width="250"
        />
        <el-table-column
          align="center"
          prop="user.nickname"
          label="管理员"
          width="80"
        />
        <el-table-column
          align="center"
          prop="create_time"
          label="申请时间"
          width="250"
        />
        <el-table-column
          align="center"
          prop="status"
          label="是否启用"
          width="80"
        >
          <template slot-scope="scope">
            <el-button
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id, 'status', scope.row.status)"
              >{{ scope.row.status === 1 ? "是" : "否" }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          width="400"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
            v-if="scope.row.apply_status == 1"
              size="small"
              type="success"
              @click="commission_click(scope.row)"
              >分佣</el-button
            >
            <el-button
              v-if="scope.row.apply_status == 0"
              size="small"
              type="success"
              @click="onApply(scope.row.id)"
              >通过审核</el-button
            >
            <el-button
              v-if="scope.row.apply_status == 0"
              size="small"
              type="warning"
              @click="onRefuse(scope.row.id)"
              >拒绝</el-button
            >
            <router-link :to="'./form/' + scope.row.id" style="margin: 0 10px">
              <el-button size="small" type="primary" icon="el-icon-edit" />
            </router-link>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="onDel(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 通过审核弹窗 -->
    <el-dialog title="通过审核" :visible.sync="dialogFormVisible" :width="isMobile()?'80%':'30%'">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="分润比" prop="divideRatio">
          <el-input
            v-model="form.divide_ratio"
            placeholder="请输入分润比"
            class="form-width"
          ></el-input
          >%
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogClose()">取 消</el-button>
        <el-button type="primary" @click="pushFromAdmin()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 拒绝弹窗 -->
    <el-dialog title="拒绝" :visible.sync="dialogFormVisible1" :width="isMobile()?'80%':'30%'">
      <el-form
        ref="formRefuse"
        :model="formRefuse"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item label="拒绝理由" prop="msg">
          <el-input
            v-model="formRefuse.msg"
            placeholder="请输入拒绝理由"
            style="width:100%"
            type="textarea"
            :rows="4"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCloseRefuse()">取 消</el-button>
        <el-button type="primary" @click="pushFromRefuse()">确 定</el-button>
      </div>
    </el-dialog>
<drawer title="分佣记录" :isShow="commission_visible"  @update:isShow="commission_visible=false" width=".8">
      <div class="flex_start flex_row">
        <el-select
          v-model="commission_form.map.status"
          placeholder="请选择"
          @change="statusOptChange"
        >
          <el-option
            v-for="item in status_options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <div class="fyye">
          分佣余额: <span style="color: red">{{ brokerage }}</span>
        </div>
      </div>

      <el-table
        :data="commission_data"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%; margin-top: 10px"
      >
        <el-table-column align="center" prop="id" label="分佣编号" />
        <el-table-column align="center" prop="order_sn" label="订单号" />
        <el-table-column align="center" prop="level" label="分佣等级" />
        <el-table-column align="center" prop="money" label="分佣金额" />
        <el-table-column align="center" prop="remark" label="描述" />
        <el-table-column align="center" prop="status_str" label="状态" />
        <el-table-column
          align="center"
          prop="trigger_user"
          label="分佣人"
          width="100"
        >
          <template slot-scope="scope">
            {{ scope.row.trigger_user && scope.row.trigger_user.nickname }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="update_time" label="修改时间" />
      </el-table>
      <el-pagination
        :current-page="commission_current_page"
        :page-size="commission_per_page"
        layout="total,prev, pager, next, jumper"
        :total="commission_total"
        @size-change="commissionSizeChange"
        @current-change="commissionCurrentChange"
      />
</drawer>
  </div>
</template>

<script>
import control from "../../components/control/control";
export default {
  name:'community_buy_index',
  components: { control },
  data() {
    return {
      config: [
        { type: "text", placeholder: "请输入标题", field: "title", val: "" },
      ],
      value: "",
      searchWhere: {
        title: "",
      },
      tableData: [],
      total: 0,
      current_page: 1,
      per_page: 14,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        divide_ratio: "",
      },
      formRefuse: {
        msg: "",
      },
      formLabelWidth: "120px",
      id: "",
      input: "",
      rules: {
        divideRatio: [
          { required: true, message: "请输入分润比", trigger: "blur" },
        ],
        msg: [{ required: true, message: "请输入拒绝理由", trigger: "blur" }],
      },
      commission_data:[],
      commission_visible: false,
      commission_form: {
        map: {
          status: "",
          user_id: 0,
        },
      },
      commission_current_page: 1,
      commission_total: 0,
      commission_per_page: 14,
      status_options: [
        { value: '', label: '全部分佣' },
        { value: 0, label: '预分佣' },
        { value: 1, label: '已分佣' },
      ],
      brokerage:'',
    };
  },
  activated(){
    if(this.$getUpdate()){
      this.onSearch()
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    // 查询
    handleSizeChange(val) {
      this.onSearch();
    },
    handleCurrentChange(val) {
      this.current_page = val;
      this.onSearch();
    },
    onApply(id) {
      this.dialogFormVisible = true;
      this.form.id = id;
    },
    // 通过审核提交
    pushFromAdmin(id) {
      this.form.status = 1;
      this.get("admin/CommunityTeam/apply", this.form, "POST").then((e) => {
        this.$message.success("通过审核！");
        this.dialogClose();
        this.onSearch();
      });

    },
    // 拒绝提交
    pushFromRefuse() {
      this.formRefuse.status = 2;
      this.get("admin/CommunityTeam/apply", this.formRefuse, "POST").then((e) => {
          this.$message.success("已拒绝！");
          this.dialogCloseRefuse();
          this.onSearch();
        });

    },
    dialogClose() {
      this.$refs.form.resetFields();
      this.dialogFormVisible = false;
      this.form.divide_ratio = 0;
      // onSearch()
    },
    dialogCloseRefuse() {
      this.$refs.formRefuse.resetFields();
      this.dialogFormVisible1 = false;
      this.formRefuse.msg = "";
      // onSearch()
    },
    onRefuse(id) {
      this.dialogFormVisible1 = true;
      this.formRefuse.id = id;
    },
    commission_click(row) {
      this.commission_current_page = 1;
      this.commission_visible = true;
      this.user_id = row.user_id;
      this.commission_form.map.status = "";
      this.getCommission();
    },
    commissionSizeChange(val) {
      this.getCommission()
    },
    commissionCurrentChange(val) {
      this.commission_current_page = val
      this.getCommission()
    },
    getCommission() {
      this.commission_form.map.user_id = this.user_id;
      this.commission_form.page = this.commission_current_page;
      this.get("admin/CommunityTeamLogs/commissionLog", this.commission_form, "POST").then(
        (e) => {
          this.brokerage = e.brokerage;
          this.commission_data = e.list.data;
          this.commission_per_page = e.list.per_page;
          this.commission_total = e.list.total;
          this.commission_current_page = e.list.current_page;
        }
      );
    },
    statusOptChange (status) {
      this.commission_form.map.status = status
      this.getCommission()
    },
    onSearch() {
      const form = {};
      form.page = this.current_page;
      form.map = this.searchWhere;
      this.get("/admin/CommunityTeam/index", form, "POST").then((e) => {
        this.tableData = e.list.data;
        this.per_page = e.list.per_page;
        this.total = e.list.total;
        this.current_page = e.list.current_page;
      });
    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      console.log(id);
    },
    // 修改状态
    change_status(id, field, val) {
      this.update(id, val, field, "CommunityTeam").then((e) => {
        e ? this.onSearch() : "";
      });
    },
    // 删除
    onDel(id) {
      this.$msg_confirm.then((e) => {
        if (e) {
          this.get("/admin/CommunityTeam/del?id=" + id, this.form, "POST").then(
            (e) => {
              this.onSearch();
            }
          );
        }
      });
    },
  },
};
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
.flex_row {
  display: flex;
  align-items: center;

}
.flex_start {
  justify-content: flex-start;
}
.fyye {
  margin-left:10px;
}
</style>>
